package com.edu.service.impl;

import com.edu.mapper.UserMapper;
import com.edu.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @Author:mx
 * @Date:2025/11/18-9:28:03
 */
@Service
public class UserServiceImpl2 implements UserService {

    @Resource
    private UserMapper userMapper;

    @Override
//    @Transactional(timeout = 5,propagation = Propagation.REQUIRED)
    public int transferMoney(int userId1, int userId2, double money) {

        int result1 = userMapper.updateUserMoney(userId1,-money);
        System.out.println(result1);
        //某些原因导致出错
//        System.out.println(1/0);
        try {
            Thread.sleep(6000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        int result2 = userMapper.updateUserMoney(userId2,money);
        System.out.println(result2);
        return result1+result2 == 2 ? 1:0;
    }
}
